/*
 * FileName:    Constants.java
 * Description: Constants 实现
 * History:     2010-8-23 (Liaobj) 1.0 Create
 */
package com.sn.system.common.constants;

import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public final class Constants {
    /** yyyy-MM-dd HH:mm:ss  */
    public static final SimpleDateFormat DATE_TIME_FORMAT =
        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /** yyyy/MM/dd HH:mm */
    public static final SimpleDateFormat DATE_MINUTE_FORMAT =
        new SimpleDateFormat("yyyy/MM/dd HH:mm");
    /** yyyy-MM-dd */
    public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
    /** yyyy/MM/dd */
    public static final SimpleDateFormat DATE_FORMAT2 = new SimpleDateFormat("yyyy/MM/dd");
    /** yyyy年MM月dd日 */
    public static final SimpleDateFormat DATE_FORMAT3 = new SimpleDateFormat("yyyy年MM月dd日");
    /** yyyyMMdd*/
    public static final SimpleDateFormat DATE_FORMAT4 = new SimpleDateFormat("yyyyMMdd");

    public static final SimpleDateFormat DATE_FORMAT6 = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",Locale.US);

    /** yyyyMMdd*/
    public static final SimpleDateFormat DATE_FORMAT7 = new SimpleDateFormat("yyyyMMddHHmmss");

    /** MM月dd日  */
    public static final SimpleDateFormat DATE_FORMAT5 = new SimpleDateFormat("MM月dd日");
    /** */
    public static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#0.######");


    private Constants() {
        //
    }

    /**
     *
     * @param date
     * @return
     */
    public static String formatTime(Date date) {
        return date == null ? "" : Constants.DATE_TIME_FORMAT.format(date);
    }

    /**
     * @param date
     * @return
     */
    public static String formatDate(Date date) {
        return date == null ? "" : Constants.DATE_FORMAT.format(date);
    }
    /**
     * @param date
     * @param format
     * @return
     */
    public static String formatDate(Date date, SimpleDateFormat format) {
        if (date == null) {
            return "";
        }
        if (format == null) {
            format = Constants.DATE_FORMAT;
        }
        return format.format(date);
    }
    /**
     * Returns MD5-tarkistussumman merkkijonosta.
     *
     * @param     s  merkkijono, josta tarkistussumma lasketaan
     * @return    MD5 summa 32 heksadesimaalin muodostamana merkkijonona
     */
    public static String md5Encoding(String s) {
        if (s == null) {
            return null;
        }
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (java.security.NoSuchAlgorithmException ex) {
            //
            throw new AssertionError(ex);
        }
        byte[] res = md.digest(s.getBytes());
        return Constants.dumpBytes(res);
    }

    /**
     * Returns heksadesimaalit tavutaulukosta merkkijonona.
     *
     * @param     bytes taulukko, jonka tavuista heksadesimaalit otetaan
     * @return    merkkijono, jossa jokaista taulukon tavua vastaa
     *            kaksi heksadesimaalia.
     */
    private static String dumpBytes(byte[] bytes) {
        int size = bytes.length;
        StringBuffer sb = new StringBuffer(size * 2);
        String s;
        for (int i = 0; i < size; ++i) {
            s = Integer.toHexString(bytes[i]);
            if (s.length() == 8) {          // -128 <= bytes[i] < 0
                sb.append(s.substring(6));
            } else if (s.length() == 2) {   // 16 <= bytes[i] < 128
                sb.append(s);
            } else {
                sb.append("0" + s);         // 0 <= bytes[i] < 16
            }
        }
        return sb.toString();
    }

    /**
     * yyyy-mm-ddからDateへ変換
     * @param dateString
     * @return Date
     */
    public static Date convertStringToDate(String dateString) {
        String[] dateStrArray = dateString.split("-");
        return new GregorianCalendar(Integer.parseInt(dateStrArray[0]),
                Integer.parseInt(dateStrArray[1]) - 1,
                Integer.parseInt(dateStrArray[2])).getTime();
    }

    /**
     * yyyy/mm/ddからDateへ変換
     * @param dateString
     * @return Date
     */
    public static Date convertStringToDate2(String dateString) {
    	Date date=null;
    	if(dateString!=null){
    		 try {
    	        	date= DATE_FORMAT2.parse(dateString);
    			} catch (ParseException e) {
    				e.printStackTrace();
    			}
    	}     
		return date;
    }
    /**
     * yyyy/mm/ddからDateへ変換
     * @param dateString
     * @return Date
     */
    public static Date convertStringToDate4(String dateString) {
    	Date date=null;
    	if(dateString!=null){
    		 try {
    	        	date= DATE_FORMAT4.parse(dateString);
    			} catch (ParseException e) {
    				e.printStackTrace();
    			}
    	}    
       
			return date;
    	}

}
